package com.hrm.controller;

import com.hrm.entity.Employee;
import com.hrm.interceptor.Token;
import com.hrm.service.EmployeeService;
import com.hrm.utils.EmailVerifyCode;
import com.hrm.utils.JsonMsg;
import org.springframework.beans.factory.annotation.Autowired;
import com.hrm.utils.setCookie;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @Auther:
 * @Date: 2020/5/20 09:53
 * @Description: 登录,注册,找回密码
 */
@Controller
public class LoginController extends BaseController {
    @Autowired
    EmployeeService employeeService;

    /**
     * 进入登录页面
     * @return: String
     */
    @RequestMapping("/login.html")
    public String toLogin(){
        return "login";
    }

    /**
     * 进入注册页面
     * @return: String
     */
    @RequestMapping("/registe.html")
    public String toRegiste(){
        return "registe";
    }

    /**
     * 进入登录页面,开启Token注解
     * @return: String
     */
    @RequestMapping("/forget.html")
    @Token(save = true)
    public String toForget(){
        return "forget";
    }

    /**
     * 退出登录
     * @return: void
     */
    @RequestMapping("/loginout.html")
    public void loginOut(){
        session.removeAttribute("user");
        //注销后重定向到登录页面
        try {
            response.sendRedirect("/login.html");
        } catch (IOException e) {
            System.out.println("跳转错误");
        }
    }


    /**
     * 登录验证
     * @param employee
     * @param remember
     * @return: JsonMsg
     */
    @RequestMapping("/loginCheck.html")
    @ResponseBody
    public JsonMsg login(Employee employee,@RequestParam(value = "remember",required = false)String remember){
        Employee employeeSession = employeeService.loginCheck(employee);
        if(employeeSession != null){
            session.setAttribute("user",employeeSession);
            System.out.println(remember);
            setCookie.setUserLoginCookie(employee.getLoginName(),employee.getPassword(),remember,request,response);
            return JsonMsg.success();
        }else {
            return JsonMsg.fail().addInfo("LOGIN_ERROR","输入的账号或密码不正确");
        }
    }

    /**
     * 注册用户
     * @param employee
     * @return: JsonMsg
     */
    @RequestMapping("/registeCheck.html")
    @ResponseBody
    public JsonMsg registe(Employee employee){
        if(!employeeService.checkName(employee)){
           return JsonMsg.fail().addInfo("REGISTE_ERROR","注册失败,账号已存在!");
        }
        if(!employeeService.checkEmail(employee)){
            return JsonMsg.fail().addInfo("REGISTE_ERROR","注册失败,邮箱已存在!");
        }
        if(!employeeService.checkPhone(employee)){
            return JsonMsg.fail().addInfo("REGISTE_ERROR","注册失败,电话已存在!");
        }
        if(employeeService.Registe(employee)){
            return JsonMsg.success();
        }
        return JsonMsg.fail().addInfo("REGISTE_ERROR","注册失败");
    }

    /**
     * 邮箱验证
     * @param employee
     * @return: JsonMsg
     */
    @RequestMapping("/emailCheck.html")
    @ResponseBody
    public JsonMsg emailCheck(Employee employee){
        if(employeeService.checkEmail(employee)){
            return JsonMsg.fail().addInfo("FORGET_ERROR","该邮箱不存在!");
        }else return JsonMsg.success();
    }

    /**
     * 发送邮箱验证
     * @param employee
     * @return: JsonMsg
     */
    @RequestMapping("/sendVerifyCodeToEmail.html")
    @ResponseBody
    public JsonMsg sendCodeToEmail(Employee employee){
        EmailVerifyCode emailVerifyCode = employeeService.sendCodeToEmail(employee.getEmail());
        session.setAttribute("emailVerifyCode",emailVerifyCode);
        return JsonMsg.success();
    }

    /**
     * 验证码验证(关闭token)
     * @param code
     * @param employee
     * @return: JsonMsg
     */
    @RequestMapping("/emailCodeTest.html")
    @Token(remove = true)
    @ResponseBody
    public JsonMsg emailCodeTest(@RequestParam(value = "code") String code,Employee employee){
        if(employeeService.emailCodeTest(code,session)){
            employeeService.resetPwd(employee);
            return JsonMsg.success();
        }else return JsonMsg.fail().addInfo("TEST_ERROR","验证码错误!");
    }

}
